// BuildQueueItem.cs
//
// Copyright (c) 2007 Mike Strobel
//
// This source code is subject to the terms of the Microsoft Reciprocal License (Ms-RL).
// For details, see <http://www.opensource.org/licenses/ms-rl.html>.
//
// All other rights reserved.

using System;
using System.ComponentModel;

using Supremacy.IO.Serialization;
using Supremacy.Resources;

namespace Supremacy.Economy
{
    /// <summary>
    /// Represents an entry in a production center's build queue.
    /// </summary>
    [Serializable]
    public class BuildQueueItem : IOwnedDataSerializableAndRecreatable, INotifyPropertyChanged
    {
        private BuildProject _project;
        private int _count;

        /// <summary>
        /// Gets the number of equivalent projects that are enqueued.
        /// </summary>
        /// <value>The number of equivalent projects.</value>
        public int Count
        {
            get { return _count; }
        }

        public string Description
        {
            get
            {
                if (this.Project == null)
                    return null;

                if (_count > 1)
                    return String.Format("{0}x {1}", _count, this.Project.Description);

                return this.Project.Description;
            }
        }

        /// <summary>
        /// Gets the queued project.
        /// </summary>
        /// <value>The queued project.</value>
        public BuildProject Project
        {
            get { return _project; }
        }

        public BuildQueueItem() {}

        /// <summary>
        /// Initializes a new instance of the <see cref="BuildQueueItem"/> class.
        /// </summary>
        /// <param name="project">The queued project.</param>
        public BuildQueueItem(BuildProject project)
        {
            if (project == null)
                throw new ArgumentNullException("project");
            _project = project;
            _count = 1;
            OnPropertyChanged("Count");
            OnPropertyChanged("Project");
        }

        /// <summary>
        /// Increments the <see cref="Count"/> property.
        /// </summary>
        /// <returns><c>true</c> if successful; otherwise, <c>false</c>.</returns>
        public bool IncrementCount()
        {
            if (_project.CloneEquivalent() != null)
            {
                _count++;
                OnPropertyChanged("Count");
                OnPropertyChanged("Description");
                return true;
            }
            return false;
        }

        /// <summary>
        /// Decrements the <see cref="Count"/> property.
        /// </summary>
        /// <returns><c>true</c> if successful; otherwise, <c>false</c>.</returns>
        public bool DecrementCount()
        {
            if (_count > 1)
            {
                _count--;
                OnPropertyChanged("Count");
                OnPropertyChanged("Description");
                return true;
            }
            return false;
        }

        #region INotifyPropertyChanged Members
        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the <see cref="PropertyChanged"/> event.
        /// </summary>
        /// <param name="propertyName">Name of the property that changed.</param>
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

    	public void SerializeOwnedData(SerializationWriter writer, object context)
    	{
    		writer.WriteOptimized(_count);
			writer.WriteObject(_project);
    	}

    	public void DeserializeOwnedData(SerializationReader reader, object context)
    	{
    		_count = reader.ReadOptimizedInt32();
			_project = reader.Read<BuildProject>();
    	}
    }
}
